package com.wxw.BaseLibrary.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.wxw.BaseLibrary.drawable.PanelStyle;
import com.wxw.BaseLibrary.element.JsonObject;
import com.wxw.BaseLibrary.element.JsonProperty;

/**
 * @author david
 * @date 2020-03-18
 */
public class JsonDataAdapter extends BaseAdapter {
    public static String KEY = "key";
    public static String LABEL = "label";
    public static String ID = "id";
    public static String SET = "set";
    public static String SHOW = "show";
    private Context _context=null;
    private LayoutInflater _inflater = null;
    private int index = -1;
    private JsonProperty jsonObjects = null;
    private IJsonDataItemViewCreator creator = null;

    public JsonDataAdapter(Context context, JsonProperty data, IJsonDataItemViewCreator creator) {
        this._context = context;
        _inflater = LayoutInflater.from(_context);
        jsonObjects = data;
        this.creator = creator;
    }

    @Override
    public int getCount() {
        return jsonObjects != null? jsonObjects.getCount():0;
    }

    @Override
    public Object getItem(int position) {
        return jsonObjects == null || position<0 || position>=jsonObjects.getCount()?null:jsonObjects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        JsonObject jsonObject = jsonObjects.get(position).getObject();
        if (creator != null) {
            convertView = creator.createView(position, convertView, _inflater, jsonObject);
        }
        if (convertView != null) {
            if (position == index) {
                convertView.setBackground(PanelStyle.createSkyBluePanelStyle());
            } else {
                convertView.setBackgroundColor(Color.WHITE);
            }
        }
        return convertView;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    public static JsonObject createSetting(String key, String label, int id) {
        return createSetting(key, label, id, true);
    }

    public static JsonObject createSetting(String key, String label, int id, boolean set) {
        return createSetting(key, label, id, set, false);
    }

    public static JsonObject createSetting(String key, String label, int id, boolean set, boolean show) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.setProperty(KEY, new JsonProperty(key));
        jsonObject.setProperty(LABEL, new JsonProperty(label));
        jsonObject.setProperty(ID, new JsonProperty(id));
        jsonObject.setProperty(SET, new JsonProperty(set));
        jsonObject.setProperty(SHOW, new JsonProperty(show));
        return jsonObject;
    }
}
